package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Care;
import com.woniuxy.oldmanapi.entity.Changecare;
import com.woniuxy.oldmanapi.service.ChangeCareService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.ChangeCareVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ChangeCareController {
    @Autowired
    ChangeCareService changeCareService;

    @GetMapping("/changeCareList/list")
    public ResponseResult<PageInfo<ChangeCareVo>> selectAllListYsw(String name, Integer now, Integer size) {

        ResponseResult<PageInfo<ChangeCareVo>> result = new ResponseResult<>();
        try {
            PageInfo<ChangeCareVo> list = changeCareService.selectAllListYsw(name, now, size);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);
        }
        return result;
    }

    @GetMapping("/change/selectOlderCareIdById/{id}")
    public ResponseResult<Care> selectOlderCareIdById(@PathVariable("id") int id) {

        ResponseResult<Care> result = new ResponseResult<>();
        try {
            Care care = changeCareService.selectOlderCareIdById(id);
            result.setMsg("选择成功");
            result.setCode(200);
            result.setData(care);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("选择失败");
            result.setCode(201);
        }
        return result;
    }

    @DeleteMapping("/change/deleteChange/{id}")
    public ResponseResult<Void> deleteChange(@PathVariable("id") int id) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            changeCareService.deleteChange(id);
            result.setMsg("删除成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("删除失败");
            result.setCode(201);
        }
        return result;
    }
    @PostMapping("/change/addChange")
    public ResponseResult<Void> addChange(@RequestBody Changecare changecare) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            changeCareService.addChange(changecare);
            result.setMsg("更换成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更换失败");
            result.setCode(201);
        }
        return result;
    }
}
